package cn.yangliu.shiro.config;

import cn.yangliu.shiro.ShiroConst;
import cn.yangliu.shiro.ShiroFormAuthenticationFilter;
import cn.yangliu.shiro.ShiroRealm;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Protocol;

/**
 * 作者 杨柳
 * 创建时间 2017-09-04 20:59
 */
@ConfigurationProperties(prefix = "spring.shiro")
public class ShiroProperties implements EnvironmentAware {
    private Environment environment;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    private int redisDatabase = 0;

    private int redisExpire = 36000;

    private String redisHost = "127.0.0.1";

    private int redisPort = 6379;

    private int redisTimeout = 2000;

    private String redisPassword = null;

    private long globalSessionTimeout = 1800000L;

    private String usernameParam = "username";

    private String passwordParam = "password";

    private String cookieName = "jsid";

    private String urlPattern = "/*";

    private String loginUrl = "/login";

    private String loginView = "login.jsp";

    private String loginErrorKey = "login_error";

    private String loginErrorMsg = "用户名或密码错误";

    private String successUrl = "/";

    private String filterChainDefinition = "/logout=logout;/js/**=anon;/css/**=anon;/res/**=anon;/static/**=anon;/**=perms,authc";

    private String hashAlgorithmName = "md5";

    private int hashIterations = 1;

    private String authErrorView = "authError";

    private String authErrorKey = "auth_error";

    private String authErrorMsg = "您没有该权限,请联系管理员授权";

    private Class<? extends AuthorizingRealm> realmClassName = ShiroRealm.class;

    private Class<? extends FormAuthenticationFilter> authFormFilterClassName = ShiroFormAuthenticationFilter.class;

    public String getLoginView() {
        return loginView;
    }

    public void setLoginView(String loginView) {
        this.loginView = loginView;
        if (!StringUtils.isEmpty(loginView)) {
            ShiroConst.LOGIN_VIEW = loginView;
        }
    }

    public String getLoginErrorKey() {
        return loginErrorKey;
    }

    public void setLoginErrorKey(String loginErrorKey) {
        this.loginErrorKey = loginErrorKey;
        if (!StringUtils.isEmpty(loginErrorKey)) {
            ShiroConst.LOGIN_ERROR_KEY = loginErrorKey;
        }
    }

    public String getLoginErrorMsg() {
        return loginErrorMsg;
    }

    public void setLoginErrorMsg(String loginErrorMsg) {
        this.loginErrorMsg = loginErrorMsg;
        if (!StringUtils.isEmpty(loginErrorMsg)) {
            ShiroConst.LOGIN_ERROR_MSG = loginErrorMsg;
        }
    }

    public String getAuthErrorKey() {
        return authErrorKey;
    }

    public void setAuthErrorKey(String authErrorKey) {
        this.authErrorKey = authErrorKey;
        if (!StringUtils.isEmpty(authErrorKey)) {
            ShiroConst.AUTH_ERROR_KEY = authErrorKey;
        }
    }

    public String getAuthErrorMsg() {
        return authErrorMsg;
    }

    public void setAuthErrorMsg(String authErrorMsg) {
        this.authErrorMsg = authErrorMsg;
        if (!StringUtils.isEmpty(authErrorMsg)) {
            ShiroConst.AUTH_ERROR_MSG = authErrorMsg;
        }
    }

    public String getAuthErrorView() {
        return authErrorView;
    }

    public void setAuthErrorView(String authErrorView) {
        if (!StringUtils.isEmpty(authErrorView)) {
            ShiroConst.AUTH_ERROR_VIEW = authErrorView;
        }
        this.authErrorView = authErrorView;
    }

    public int getRedisDatabase() {
        if (environment.containsProperty("spring.shiro.redis-database")) {
            return redisDatabase;
        } else if (environment.containsProperty("spring.redis.database")) {
            redisDatabase = environment.getProperty("spring.redis.database", Integer.class);
            return redisDatabase;
        }
        return redisDatabase;
    }

    public void setRedisDatabase(int redisDatabase) {
        this.redisDatabase = redisDatabase;
    }

    public int getRedisExpire() {
        return redisExpire;
    }

    public void setRedisExpire(int redisExpire) {
        this.redisExpire = redisExpire;
        ShiroConst.EXPIRE = redisExpire;
    }

    public long getGlobalSessionTimeout() {
        return globalSessionTimeout;
    }

    public void setGlobalSessionTimeout(long globalSessionTimeout) {
        this.globalSessionTimeout = globalSessionTimeout;
    }

    public String getUsernameParam() {
        return usernameParam;
    }

    public void setUsernameParam(String usernameParam) {
        this.usernameParam = usernameParam;
        ShiroConst.USERNAME_PARAM = usernameParam;
    }

    public String getPasswordParam() {
        return passwordParam;
    }

    public void setPasswordParam(String passwordParam) {
        this.passwordParam = passwordParam;
        ShiroConst.PASSWORD_PARAM = passwordParam;
    }

    public String getCookieName() {
        return cookieName;
    }

    public void setCookieName(String cookieName) {
        this.cookieName = cookieName;
    }

    public String getUrlPattern() {
        return urlPattern;
    }

    public void setUrlPattern(String urlPattern) {
        this.urlPattern = urlPattern;
    }

    public String getLoginUrl() {
        return loginUrl;
    }

    public void setLoginUrl(String loginUrl) {
        this.loginUrl = loginUrl;
    }

    public String getSuccessUrl() {
        return successUrl;
    }

    public void setSuccessUrl(String successUrl) {
        this.successUrl = successUrl;
        if (!StringUtils.isEmpty(successUrl)) {
            ShiroConst.SUCCESS_URL = successUrl;
        }
    }

    public String getFilterChainDefinition() {
        return filterChainDefinition;
    }

    public void setFilterChainDefinition(String filterChainDefinition) {
        this.filterChainDefinition = filterChainDefinition;
    }

    public String getHashAlgorithmName() {
        return hashAlgorithmName;
    }

    public void setHashAlgorithmName(String hashAlgorithmName) {
        this.hashAlgorithmName = hashAlgorithmName;
    }

    public int getHashIterations() {
        return hashIterations;
    }

    public void setHashIterations(int hashIterations) {
        this.hashIterations = hashIterations;
    }

    public Class<? extends AuthorizingRealm> getRealmClassName() {
        return realmClassName;
    }

    public void setRealmClassName(Class<? extends AuthorizingRealm> realmClassName) {
        this.realmClassName = realmClassName;
    }

    public Class<? extends FormAuthenticationFilter> getAuthFormFilterClassName() {
        return authFormFilterClassName;
    }

    public void setAuthFormFilterClassName(Class<? extends FormAuthenticationFilter> authFormFilterClassName) {
        this.authFormFilterClassName = authFormFilterClassName;
    }


    public String getRedisHost() {
        if (environment.containsProperty("spring.shiro.redis-host")) {
            return redisHost;
        } else if (environment.containsProperty("spring.redis.host")) {
            return environment.getProperty("spring.redis.host");
        }
        return "127.0.0.1";
    }

    public int getRedisPort() {
        if (environment.containsProperty("spring.shiro.redis-port")) {
            return redisPort;
        } else if (environment.containsProperty("spring.redis.port")) {
            return environment.getProperty("spring.redis.port", Integer.class);
        }
        return 6379;
    }

    public int getRedisTimeout() {
        if (environment.containsProperty("spring.shiro.redis-timeout")) {
            return redisTimeout;
        } else if (environment.containsProperty("spring.redis.timeout")) {
            return environment.getProperty("spring.redis.timeout", Integer.class);
        }
        return Protocol.DEFAULT_TIMEOUT;
    }

    public String getRedisPassword() {
        if (environment.containsProperty("spring.shiro.redis-password")) {
            return redisPassword;
        } else if (environment.containsProperty("spring.redis.password")) {
            return environment.getProperty("spring.redis.password");
        }
        return null;
    }

    public void setRedisHost(String redisHost) {
        this.redisHost = redisHost;
    }

    public void setRedisPort(int redisPort) {
        this.redisPort = redisPort;
    }

    public void setRedisTimeout(int redisTimeout) {
        this.redisTimeout = redisTimeout;
    }

    public void setRedisPassword(String redisPassword) {
        this.redisPassword = redisPassword;
    }
}
